// @ts-nocheck
import router from '@system.router';
import prompt from '@system.prompt';
import storage from '@system.storage';

export default {
    data: {
        title: 'World',
        mode:'',
        noteInfo: {
            title: '123123'
        },
        finishBtnVisible:false
    },
    onInit() {
        console.log("来到详情");
        console.log("获取索引：" + this.noteInfo.index);
        console.log(this.noteInfo.title);
    },

    /************** 自定义的方法区 ****************/
    goBack() {
        router.back();
    },
    changeContent(e){
        this.noteInfo.content = e.text;
    },
    showFinishBtn(){
        this.finishBtnVisible = true;
    },
    getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    },
    saveNote(){
        let that = this;
        let noteInfoIndex = this.noteInfo.index;
        console.log("保存数据");
        storage.get({
            key: 'toDoList',
            success: function (data) {
                console.log("修改后的数据："+that.noteInfo.content);
                let toDoList = JSON.parse(data)
                //如果是新增模式，则新增数据
                if(that.mode === 'add'){
                    console.log('新增模式');
                    that.noteInfo.date = that.getNowFormatDate();
                    toDoList.push(that.noteInfo);
                }
                else{
                    console.log('编辑模式')
                    toDoList[noteInfoIndex] = that.noteInfo;
                }
                storage.set({
                    key: 'toDoList',
                    value: JSON.stringify(toDoList),
                    success: function (data) {
                        router.back();
                    }
                });

            }
        })
    },
    deleteNote() {
        let noteInfoIndex = this.noteInfo.index;
        prompt.showDialog({
            title: '操作确认',
            message: '确定要删除此便签吗？',
            buttons: [
                {
                    text: '删除',
                    color: 'red',
                },
                {
                    text: '取消',
                    color: '#ffa704',
                },
            ],
            success: function (data) {
                //点击确定按钮后执行操作
                if (data.index === 0) {
                    /**
                     * 1.从storage中获取数据并解析成数组
                     * 2.删除指定元素
                     * 3.将删除元素后的数组重新保存到storge
                     * 4.回退到列表页面
                     * */
                    let that = this;
                    storage.get({
                        key: 'toDoList',
                        success: function (data) {
                            let toDoList = JSON.parse(data)
                            toDoList.splice(noteInfoIndex, 1);
                            storage.set({
                                key: 'toDoList',
                                value: JSON.stringify(toDoList),
                                success: function (data) {
                                    router.back();
                                }
                            });
                        }
                    })
                }
            },
        });
    }
}
